import pandas as pd
import os

root = "output/ensemble/"
res_fpn = pd.read_csv(root + "res_ori_fpn.csv")
res_fpn=res_fpn[res_fpn["confindence"]>=0.4]
print(res_fpn.shape)

res_fpn_dcn = pd.read_csv(root +"res_ori_fpn_dcn.csv")
res_fpn_dcn=res_fpn_dcn[res_fpn_dcn["confindence"]>=0.4]
print(res_fpn_dcn.shape)

res_rfcn_dcn = pd.read_csv(root +"res_ori_rfcn_dcn.csv")
res_rfcn_dcn=res_rfcn_dcn[res_rfcn_dcn["confindence"]>=0.4]
print(res_rfcn_dcn.shape)

# res_faster_rcnn = pd.read_csv(root +"faster_rcnn.csv")
# res_faster_rcnn=res_faster_rcnn[res_faster_rcnn["confindence"]>=0.4]
# res_faster_rcnn = res_faster_rcnn[["filename","minx","miny","maxx","maxy","confindence","image_id"]]
# print(res_faster_rcnn.shape)


res_kohill = pd.read_csv(root +"kohill_res.csv",sep=',')
res_kohill=res_kohill[res_kohill["confindence"]>=0.8]
res_kohill_deal = res_kohill[["filename","minx","miny","maxx","maxy","confindence","image_id"]]
print(res_kohill_deal.shape)

res_retinanet = pd.read_csv(root +"retinanet_res.csv",sep=',')
res_retinanet=res_retinanet[res_retinanet["confindence"]>=0.5]
res_retinanet_deal = res_retinanet[["filename","minx","miny","maxx","maxy","confindence","image_id"]]
print(res_retinanet_deal.shape)


res = pd.concat([res_fpn,res_fpn_dcn,res_rfcn_dcn,res_kohill_deal,res_retinanet_deal])
print(res.shape[0])
print(res.head(10))
import numpy as np
final =pd.DataFrame()

import tqdm
for i in tqdm.tqdm((res['filename'].unique())):
    dealtrain= res[res['filename'] == i]
    dealtrain['flg']=0
    dealtrain_np = np.array(dealtrain.loc[:, ('minx','miny','maxx','maxy','flg')]).astype('f')
    for j in range(dealtrain.shape[0]-1):
        if (dealtrain_np[j, 4]==0):
            dealtrain_np[j, 4] = 2
            jl=1
            x11 = dealtrain_np[j,0]
            x12 = dealtrain_np[j,2]
            y11 = dealtrain_np[j,1]
            y12 = dealtrain_np[j,3]
            for k in range(j+1,dealtrain.shape[0]):
                x21 = dealtrain_np[k,0]
                x22 = dealtrain_np[k,2]
                y21 = dealtrain_np[k,1]
                y22 = dealtrain_np[k,3]
                sum1 = (x12 - x11) * (y12 - y11)
                sum2 = (x22 - x21) * (y22 - y21)
                sum = min(sum1, sum2)
                x1 = max(x11, x21)
                x2 = min(x12, x22)
                y1 = max(y11, y21)
                y2 = min(y12, y22)
                if (x2 > x1) & (y2 > y1):
                    sumbj = (x2 - x1) * (y2 - y1)
                else:
                    sumbj = 0

                if (float(sumbj) / float(sum) >= 0.9):

                    jl =jl+1
                    dealtrain_np[k, 4] = 1
                    dealtrain_np[j, :4] = dealtrain_np[j, :4] + dealtrain_np[k, :4]

        if (jl>=3):
            dealtrain_np[j, :4] /= jl
        else:
            dealtrain_np[j, 4] = 1

    dealtrain.loc[:, ('minx','miny','maxx','maxy','flg')] = dealtrain_np[:]
    dealtrain = dealtrain[dealtrain['flg']==2]
    final = pd.concat([final,dealtrain])
    # print(final)

print(final.shape)
final.to_csv(root+"res_combine_kohill_vote3.csv",index=False)




